package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.PasswordErrorException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 */
public class MainController {
    private Scanner sc = new Scanner(System.in);
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
    private UserService userService = (UserService) ObjectFactory.getObject("userService");

    private User user;
    // 当用户成功登录之后,会创建一个当前类的对象
    // 每次创建对象后在该对象的属性中都会创建一个购物车对象
    // 用于表示分配个体当前登录的用户的购物车
    private Cart cart = new Cart();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Cart getCart() {
        return cart;
    }

    public void setCart(Cart cart) {
        this.cart = cart;
    }

    public MainController(User user) {
        this.user = user;
    }

    public void showMenu() {
        System.out.println("*********************欢迎" + user.getUsername() + "成功进入紫金商城主页*********************");
        System.out.print("请选择服务(1.加入购物车 2.查看购物车 3.我的订单 4.修改密码 5.退出登录):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            case 3:
                findMyOrders();
                break;
            case 4:
                modifyPassword();
                break;
            case 5:
                System.out.println("提示:您已经退出登录!");
                new StartController().showMenu();
                break;
            default:
                System.out.println("错误:对不起,您输入的选项有误,请重新输入");
                break;
        }
    }

    public void addCart() {
        List<Product> products = productService.findAll();
        if (!products.isEmpty()) {
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
        }
        System.out.print("请输入需要加入购物车的商品编号:");
        String id = sc.next();

        try {
            cartService.addCart(cart, id);
            System.out.println("提示:恭喜您,编号为:" + id + "的商品已经成功加入购物车");
            showMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        }
    }

    public void showCart() {
        // 查看购物车业务功能
        List<Item> items = cart.getItems();
        System.out.println("商品编号\t\t商品名称\t\t商品数量\t\t总价");
        for (Item item : items) {
            System.out.println(item.getProduct().getId() + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
        }
        System.out.println("---------------------------------------------");
        System.out.println("购物车总价:" + cart.getPrice() + "元");
        cartMenu();
    }

    public void cartMenu() {
        // 二级菜单
        System.out.print("请选择服务(1.删除指定商品 2.修改商品数量 3.清空购物车 4.结算 5.回到主菜单):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                cleanCart();
                break;
            case 4:
                createNo();
                break;
            case 5:
                showMenu();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入");
                cartMenu();
                break;
        }
    }

    public void removeCart() {
        System.out.print("请输入需要删除的商品编号:");
        String id = sc.next();
        try {
            cartService.removeCart(cart, id);
            System.out.println("提示:恭喜您,删除成功");
            cartMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            cartMenu();
        }
    }

    public void modifyCart() {
        System.out.print("请输入需要修改数量的商品编号:");
        String id = sc.next();
        System.out.print("请输入修改后的明细数量:");
        String num = sc.next();

        try {
            cartService.modifyCart(cart, id, num);
            System.out.println("提示:恭喜您,修改数量成功");
            cartMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            cartMenu();
        }
    }

    public void cleanCart() {
        cart = new Cart();
        System.out.println("提示:恭喜您,成功清空购物车");
        cartMenu();
    }

    public void createNo() {
        try {
            String no = orderService.createNo(cart, user);
            System.out.println("提示:恭喜您,结算成功,您的订单号为:" + no);
            // 结算成功之后需要清空购物车
            cart = new Cart();
            cartMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        }
    }

    public void findMyOrders() {
        try {
            // 展示属于当前登录的用户的所有订单信息
            List<Order> orders = orderService.findMyOrders(user);
            System.out.println("编号\t\t订单号\t\t总价");
            for (Order order : orders) {
                System.out.println(order.getId() + "\t\t" + order.getNo() + "\t\t" + order.getPrice());
            }
            orderMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        }
    }


    public void orderMenu() {
        // 存在二级菜单
        System.out.print("请选择服务(1.查看订单详情 2.返回主菜单):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                findOrderDetail();
                break;
            case 2:
                showMenu();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入");
                orderMenu();
                break;
        }
    }

    public void findOrderDetail() {
//        用户名:xxx
//        手机号:xxxxx
//        订单号:xxxxxxxxxx
//        -------------------------------------------
//        序号	商品名称	数量	总价
//        1
//        2
//        3
//        4
//        ----------------------------------------------
//        共:xxx元 总计:4条
        System.out.print("请输入订单编号:");
        String id = sc.next();

        try {
            Order order = orderService.findDetail(id);
            System.out.println("用户名:" + order.getUser().getUsername());
            System.out.println("手机号:" + order.getUser().getPhone());
            System.out.println("订单号:" + order.getNo());
            System.out.println("----------------------------------------------------");
            List<Item> items = order.getItems();
            System.out.println("序号\t\t商品名称\t\t数量\t\t总价");
            for (int i = 0; i < items.size(); i++) {
                Item item = items.get(i);
                System.out.println((i + 1) + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
            }
            System.out.println("----------------------------------------------------");
            System.out.println("共:" + order.getPrice() + "元\t\t\t\t总计:" + order.getCount() + "条");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
        }
        orderMenu();
    }

    public void modifyPassword() {
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        System.out.print("请输入新密码:");
        String password = sc.next();

        try {
            userService.modifyPassword(password, oldPassword, user);
            System.out.println("提示:恭喜您,密码修改成功");
            // 密码修改成功之后需要重新登录
            new StartController().showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        } catch (PasswordErrorException e) {
            System.out.println("错误:" + e.getMessage());
            showMenu();
        } catch (UserNotExistException e) {
            System.out.println("错误:" + e.getMessage());
            new StartController().showMenu();
        }

    }
}
